﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TweetJSON.JSON.Entity;
using Newtonsoft.Json;
using TweetJSON.DAL;

namespace TweetJSON
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            //string fileName = @"E:\Rajin\Development\SVNProjects\TwitterUserStatistics\Data\data.js";
            lblMessage.Text = "Reading file";
        }
        private void execute()
        {
            string fileName = @"E:\Rajin\Development\DBMS\data1lac.js";
            List<string> fileContent = GetString(fileName);
            lblMessage.Text = "Inserting to MySql";
            TweetStatDB db = new TweetStatDB();
            int total = fileContent.Count;
            int index = 0;
           
            this.progress.Value = 0;
            foreach (string s in fileContent)
            {
                Tweet t = JsonConvert.DeserializeObject<Tweet>(s);
                db.InsertTweet(t);
                db.InsertUser(t.user);
                index++;
                int percentile = index * 100 / total;
                this.progress.Value = percentile;
                
                //lblMessage.Text = "Inserting to MySql " + percentile.ToString() + "%";
            }
            MessageBox.Show("Done");
            lblMessage.Text = "Done";
        }
        private List<string> GetString(string fileName)
        {
            lblMessage.Text = "Reading file";
            List<string> result = new List<string>();
            int index = 0;
            int total = 100001;
            using (StreamReader sr = new StreamReader(fileName))
            {
                while(result != null)
                {
                    result.Add(sr.ReadLine());
                    index++;
                    int percentile = index * 100 / total;
                    if (percentile < 100)
                    {
                        this.progress.Value = percentile;
                        //lblMessage.Text = "Reading file: "+percentile.ToString()+"%";
                    }
                    else
                    {
                        break;
                    }

                    
                }
                
            }

            return result;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            execute();
        }
    }
}
